package be.xios.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;

public class TouchControll extends View{
	private Canvas c;
	private Paint paint;
	private Paint p;
	private int steps;
	private int x;
	private int y;
	private int xPoint;
	private int yPoint;
	private Path path;
	private boolean touch = false;
//http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
	public TouchControll(Context context) {
		super(context);
		init();
	}
	 @Override
     public boolean onTouchEvent(MotionEvent event) {
		 touch = true;
         int x = (int)event.getX();
         int y = (int)event.getY();
         switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN:
            	 updatePosition(x, y);
                 break;
             case MotionEvent.ACTION_MOVE:
            	 updatePosition(x, y);
                 break;
             case MotionEvent.ACTION_UP:
            	 updatePosition(-1,-1); 
            	 touch = false;
                 break;
         }
         return true;
     }
	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(2);
		paint.setTextSize(25);
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(Color.WHITE);
		
		p = new Paint();
		p.setAntiAlias(true);
		p.setStrokeWidth(2);
		p.setTextSize(25);
		p.setStyle(Paint.Style.FILL);
		p.setColor(Color.WHITE);
		steps = 3;
		
		path = new Path();
	
	}
	@Override
	protected void onDraw(Canvas canvas) {
		c = canvas;
		xPoint = getMeasuredWidth()/2;
		yPoint = getMeasuredHeight()/2;
		path.addCircle(xPoint, yPoint, 150, Path.Direction.CW);
		c.drawPath(path, paint);
		for (int i = 0; i < steps; i++) {
			c.drawLine(xPoint-5, yPoint-(i*55), xPoint+5, yPoint-(i*55), paint);
			c.drawLine(xPoint-5, yPoint+(i*55), xPoint+5, yPoint+(i*55), paint);
			c.drawLine(xPoint+(i*55), yPoint-5, xPoint+(i*55), yPoint+5, paint);
			c.drawLine(xPoint-(i*55), yPoint-5, xPoint-(i*55), yPoint+5, paint);
		}
		if(touch ==true){
		c.drawCircle(this.x,this.y,15,p);
		c.drawText("[x:"+(x-xPoint)+" | y:"+(y-yPoint)+"]", (float)(xPoint+60), (float)(yPoint-160), paint);
		c.drawText(createInfo((x-xPoint),(y-yPoint)),(float)(xPoint+60), (float)(yPoint-190), paint);
		
		}
	}
	public String createInfo(int x,int y){
		if(x==0&&y==0){
			return "[Middle]";
		}
		if(y<0){
			if(x<0){
				return "[Top & Left]";
			}else{
				return "[Top & Right]";		
			}
		}else{
			if(x<0){
				return "[Bottom & Left]";
			}else{
				return  "[Bottom & Right]";		
			}	
		}
		
		
		
	}
	public void updatePosition(int x, int y){
		if((Math.pow((x-xPoint),2)+ Math.pow((y-yPoint),2))< Math.pow(150,2)){
			this.x= x;
			this.y = y;
		}else{
			this.x = getMeasuredWidth()/2;
			this.y = getMeasuredHeight()/2;
		}
		 invalidate();		
	}
}
